/**
 * Project Name:fire-industry-DM-center
 * File Name:TerminalMenuServer.java
 * Package Name:com.firestone.web.center
 * Date:2018年3月29日下午6:35:55
 *
*/

package com.firestone.web.center;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.terminal.TerminalDTO;
import com.firestone.bean.dto.user.User;
import com.firestone.bean.entity.mysql.primary.ImCenterTerminalMenu;
import com.firestone.bean.po.terminal.SavePO;
import com.firestone.bean.po.terminal.UpdatePO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.terminal.TerminalVO;
import com.firestone.converters.center.ConverterTerminal;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.exception.FireException;
import com.firestone.service.center.IImCenterTerminalMenuService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Description: TODO(终端菜单控制器)
 * @author CAIYJ
 * @date 2018年3月29日 下午6:35:55
 */
@Api(value = "终端菜单控制器", tags = { "终端菜单接口文档" })
@RestController
@RequestMapping(value = "/ terminal")
public class TerminalMenuServer extends BaseServer {

    @Autowired
    private IImCenterTerminalMenuService imCenterTerminalMenuService;

    /**
     * @Description: TODO(终端菜单列表)
     * @param name
     * @param pagePO
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年3月30日 下午3:00:33
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "列表", notes = "列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "菜单名称", required = false, dataType = "String", paramType = "query") })
    @RequestMapping(value = "/list")
    public ReturnDTO<BaseListVO<TerminalVO>> list(String name,
            @ApiParam PageDTO pagePO) throws Exception {
        Page<TerminalDTO> page = new Page<>(pagePO.getPage(), pagePO.getSize());
        page = imCenterTerminalMenuService.listTerminal(page, name);
        BaseListVO<TerminalVO> vo = ConverterTerminal.convertListVO(page);
        return ReturnDTO.success(vo);
    }

    /**
     * @Description: TODO(新增终端菜单)
     * @param po
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年3月30日 下午3:00:45
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/save")
    public ReturnDTO<String> save(@ApiParam SavePO po) throws Exception {
        ImCenterTerminalMenu terminal = new ImCenterTerminalMenu();
        BeanUtils.copyProperties(po, terminal);
        User user = getUserInfo();
        terminal.setCreator(user.getId());
        boolean result;
        try {
            result = imCenterTerminalMenuService.insert(terminal);
        } catch (DuplicateKeyException e) {
            throw new FireException(
                    SystemCodeEnum.error_duplicated_data.getErrorCode(),
                    "已存在同名菜单", e);
        }
        if (result) {
            return ReturnDTO.success("新增成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }

    /**
     * @Description: TODO(修改终端菜单)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年3月30日 下午3:00:57
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/update/{id}")
    public ReturnDTO<String> update(@PathVariable Long id,
            @ApiParam UpdatePO po) throws Exception {
        ImCenterTerminalMenu terminal = new ImCenterTerminalMenu();
        BeanUtils.copyProperties(po, terminal);
        User user = getUserInfo();
        terminal.setReviser(user.getId());
        terminal.setId(id);
        boolean result;
        try {
            result = imCenterTerminalMenuService.updateById(terminal);
        } catch (DuplicateKeyException e) {
            throw new FireException(
                    SystemCodeEnum.error_duplicated_data.getErrorCode(),
                    "已存在同名菜单", e);
        }
        if (result) {
            return ReturnDTO.success("修改成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_update_failed);
    }

    /**
     * @Description: TODO(删除终端菜单)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年3月30日 下午3:01:14
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/remove/{id}")
    public ReturnDTO<String> remove(@PathVariable Long id) throws Exception {
        if (imCenterTerminalMenuService.deleteById(id)) {
            return ReturnDTO.success("删除成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_delete_failed);
    }
}
